<?php
/**
 * Created by PhpStorm.
 * User: 24369
 * Date: 2021/6/25
 * Time: 10:03
 */
namespace app\admin\logic;

use app\admin\BaseController;
use app\admin\validate\GoodValidate;
use think\exception\ValidateException;
use think\facade\Filesystem;
use think\facade\Request;

class GoodLogic extends BaseController
{
    public static function getWhere()
    {
        $post = Request::param();
        $where = [];
        if (isset($post['is_show']) && $post['is_show'] != '')      $where['is_show'] = $post['is_show'];
        if (isset($post['is_hot']) && $post['is_hot'] != '')        $where['is_hot'] = $post['is_hot'];
        if (isset($post['is_down']) && $post['is_down'] != '')      $where['is_down'] = $post['is_down'];
        if (isset($post['cid']) && $post['cid'] != '')              $where['cid'] = $post['cid'];
        if (isset($post['keywords']) && $post['keywords'] != '')    $where['keywords']=$post['keywords'];
        return  $where;
    }
    public static function getGoodAdd()
    {
        $post=Request::post();
        $msg='';
        //验证器验证上传数据
        try{
            validate(GoodValidate::class)->check($post);
        }catch (ValidateException $validateException){
            $msg=$validateException->getMessage();
        }
        //图片上传
        try{
            $files=Request::file();
            validate(['img'=>'filesize:10485760|fileExt:jpg,png,jpeg,gif'])->check($files);
            $path=Filesystem::putFile('UploadFiles/product',$files['img'],'md5');
            $path=str_replace('\\','/',$path);
        }catch (ValidateException $validateException){
            $msg=$validateException->getMessage();
        }catch (\Exception $exception){
            $path='';
        }
        $post['is_show']=isset($post['is_show'])?$post['is_show']:0;
        $post['is_hot']=isset($post['is_hot'])?$post['is_hot']:0;
        $post['is_down']=isset($post['is_down'])?$post['is_down']:0;
        return  [$msg,$path,$post];
    }
    public static function getImg($file,$paths)
    {
        $msg='';
        $path='';
        try{
            $files=Request::file();
            validate([$file=>'filesize:10485760|fileExt:jpg,png,jpeg,gif'])->check($files);
            $path=Filesystem::putFile($paths,$files[$file],'md5');
            $path=str_replace('\\','/',$path);
        }catch (ValidateException $validateException){
            $msg=$validateException->getMessage();
        }catch (\Exception $exception){
            $path='';
        }

        return [$msg,$path];
    }
}